package com.msh.artascope.web.common.api;

import com.msh.artascope.web.common.model.LoginInfo;
import com.msh.artascope.web.common.model.ModifyPassword;
import com.msh.frame.client.common.Result;
import com.msh.artascope.interfaces.IAccountAuth;
import com.msh.artascope.interfaces.ILogin;
import com.msh.artascope.interfaces.ILoginAccount;
import com.msh.artascope.interfaces.IVerifyInfo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 前端修改登录请求地址
 * 例:
 * VUE_APP_LOGIN_IN = 'admin-server-api/login/in'
 * VUE_APP_LOGIN_OUT = 'admin-server-api/login/out'
 * VUE_APP_LOGIN_INFO = 'admin-server-api/login/info'
 * VUE_APP_LOGIN_IMG_VERIFY_CODE = 'admin-server-api/login/verifyCode'
 * VUE_APP_LOGIN_SYSTEM_AUTH = 'admin-server-api/login/accountAuth'
 */
@Tag(name = "登录-api相关接口")
@Slf4j
@RestController
@RequestMapping("/api/login")
@AllArgsConstructor
public class LoginInfoApi {
  private final ILogin login;

  @Operation(summary = "获取验证码")
  @GetMapping("verifyCode")
  public Result<IVerifyInfo> verifyCode() {
    return Result.successReturn(login.verifyCodeImg());
  }

  @Operation(summary = "登录")
  @PostMapping("in")
  public Result<ILoginAccount> in(@RequestBody @Validated LoginInfo loginInfo) {
    ILoginAccount loginAccount = login.loginIn(loginInfo);
    return Result.successReturn(loginAccount).setCode(2000000);
  }

  @Operation(summary = "修改密码")
  @PostMapping("modifyPassword")
  public Result modifyPassword(@RequestBody @Validated ModifyPassword modifyPassword) {
    login.modifyPassword(modifyPassword);
    return Result.successReturn();
  }

  @Operation(summary = "登出")
  @GetMapping("out")
  public Result out() {
    login.loginOut();
    return Result.successReturn();
  }

  @Operation(summary = "获取登录信息")
  @GetMapping("info")
  public Result<ILoginAccount> info() {
    ILoginAccount loginAccount = login.getLoginAccount();
    return Result.successReturn(loginAccount);
  }

  @Operation(summary = "获取账号权限")
  @PostMapping("accountAuth")
  public Result<IAccountAuth> accountAuth() {
    IAccountAuth accountAuth = login.getAccountAuth();
    return Result.successReturn(accountAuth);
  }

}
